package com.hystrix.payment.controller;


import com.hystrix.payment.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentOK(@PathVariable("id") Integer id) {
        log.info("Thread:" + Thread.currentThread().getName());
        return paymentService.payment_ok(id);
    }


    /**
     * 两万个并发会拖慢速度
     * tomcat工作线程全都挤占到此方法
     * @param id
     * @return
     */
    @GetMapping("/payment/hystrix/error/{id}")
    public String paymentERROR(@PathVariable("id") Integer id) {
        System.out.println("Thread:" + Thread.currentThread().getName());
        return paymentService.payment_timeout(id);
    }

    @GetMapping("/payment/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
        String result = paymentService.paymentCircuitBreaker(id);
        log.info("*****result: " + result);
        return result;
    }


}
